/*
 * Copyright 2004-2006 公司名
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.companyname.cn/licenses
 */

package com.companyname.common.sysframe.dao;

import java.util.List;

import org.apache.log4j.Logger;

import com.companyname.common.base.dao.BaseDAOHibernate;
import com.companyname.common.sysframe.define.RoleStatus;
import com.companyname.common.sysframe.model.Role;
import com.companyname.common.sysframe.util.CompanyNameSysFrameException;

/**
 * <p>Title: 角色</p>
 * <p>Description: 角色 Role </p>
 * <p>Copyright: Copyright (c) 2004-2006</p>
 * <p>Company: 公司名</p>
 * @ $Author: 作者名 $
 * @ $Date: 创建日期 $
 * @ $Revision: 1.0 $
 * @ created in 创建日期
 *
 */

public class RoleDAOimp extends BaseDAOHibernate
                        implements RoleDAO
{
        static Logger logger = Logger.getLogger(RoleDAOimp.class);

        /**
         * 描述:角色
         *
         * @ param:
         * @ param:orderBy 排序属性（for hql）
         * @ param:sortType 排序方式（asc or desc）
         * @ Exception:
         * @ return model集合
         */
        public List findRoles(String code,
                              String superRoleId,
                              String name,
                              String status,
                              String orderBy,
                              String sortType)

        {
                String hql = " from Role role " + 
                " where( role.code like ? ";
                if(!superRoleId.equals("%"))
                {
                        hql = hql +
                " and role.superRole.id like '" + superRoleId + "' ";
                }
                hql = hql +          
                " and role.name like ? " + 
                " and role.status = ? " +
                " and role.status != ? " +
                " ) " + 
                " order by " + orderBy + " " + sortType;
                Object[] values = new Object[4];
                values[0] = code;
                values[1] = "%" + name + "%";
                values[2] = new Integer(status);
                values[3] = new Integer(RoleStatus.BLANK_OUT);
                return this.getHibernateTemplate().find(hql,values);
        }
        
        public Role getByCode(String code)
        {
                String hql = 
                        " from Role " +
                        " where( code = ? " +
                        " and status != ? ) ";
                Object[] values = new Object[2];
                values[0] = code;
                values[1] = RoleStatus.BLANK_OUT;
                List roles =  this.getHibernateTemplate().find(hql,values);
                if(roles.size() == 0)
                {
                        return null;
                }
                if(roles.size() > 1)
                {
                        throw new CompanyNameSysFrameException("代码为" + code + "的角色有" + roles.size() + "个");
                }
                return (Role)roles.get(0);
        }
        
        public List findRoles(String superRoleId)
        {
                if(superRoleId == null)
                {
                        String hql =  
                                " from Role role " +
                                " where ( role.superRole = NULL " +
                                " and role.status = ? ) " +
                                " order by role.pri ";
                        Object[] values = new Object[1];
                        values[0] = RoleStatus.VALID;
                        return this.getHibernateTemplate().find(hql,values);
                }
                else
                {
                        String hql =  
                                " from Role " +
                                " where ( role.superRole.id = ? " +
                                " and role.status = ? ) " +
                                " order by role.pri ";
                        Object[] values = new Object[2];
                        values[0] = superRoleId;
                        values[1] = RoleStatus.VALID;
                        return this.getHibernateTemplate().find(hql,values);
                }
        }
}
